package dimension.starry.block.typeInterface;

import dimension.starry.StarryDimension;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public abstract class MachineScreen<T extends MachineContainer> extends HandledScreen<T> {

    private static final Identifier POINTER = new Identifier(
        StarryDimension.MOD_ID, "textures/gui/container/pointer.png");
    private static final Identifier FIRE = new Identifier(StarryDimension.MOD_ID, "textures/gui/container/fire.png");

    private final Identifier backgroundResource;
    private final int textureWidth;
    private final int textureHeight;

    public MachineScreen(
        T cntainer, PlayerInventory playerInventory, Text titleText, Identifier backgroundResource, int textureWidth,
        int textureHeight
    ) {
        super(cntainer, playerInventory, titleText);
        this.backgroundResource = backgroundResource;
        this.textureWidth = textureWidth;
        this.textureHeight = textureHeight;
    }

    @Override
    protected void drawBackground(DrawContext drawContext, float particleTicks, int mouseX, int mouseY) {
        renderBackground(drawContext);
        int left = (this.width - textureWidth) / 2;
        int top = (this.height - textureHeight) / 2;
        drawContext.drawTexture(
            backgroundResource, left, top, 0, 0, this.textureWidth, this.textureHeight, this.textureWidth,
            this.textureHeight
        );
    }

    @Override
    public void render(DrawContext drawContext, int mouseX, int mouseY, float particleTicks) {
        super.render(drawContext, mouseX, mouseY, particleTicks);
        this.drawMouseoverTooltip(drawContext, mouseX, mouseY);
    }

    protected void drawJianTou(DrawContext drawContext, int leftPos, int topPos) {
        MachineTileEntity machineTileEntity = this.getScreenHandler().getMachineTileEntity();
        int progress = (int) (machineTileEntity.getProgress() / (double) machineTileEntity.getConsumeTime() * 24);
        drawContext.drawTexture(POINTER, this.x + leftPos, this.y + topPos, 0, 0, progress, 17, 24, 17);
    }

    protected void drawFire(DrawContext drawContext, int leftPos, int topPos) {
        int status = this.getScreenHandler().getMachineTileEntity().getMachineStatus() ? 14 : 0;
        drawContext.drawTexture(FIRE, this.x + leftPos, this.y + topPos, 0, 0, status, 14, 14, 14);
    }

}
